/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.distillerybagger.distilleryexplorer;

import java.io.IOException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import org.openide.actions.DeleteAction;
import org.openide.actions.PropertiesAction;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.ChildFactory;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.actions.SystemAction;
import org.openide.util.lookup.Lookups;

/**
 *
 * @author clayton
 */
public class DistilleryChildFactory extends ChildFactory<Distillery> 
{

    private List<Distillery> resultList;

    public DistilleryChildFactory(List<Distillery> resultList) {
        this.resultList = resultList;
    }

    @Override
    protected boolean createKeys(List<Distillery> list) {
        for (Distillery distillery : resultList) {
            list.add(distillery);
        }
        return true;
    }

    @Override
    protected Node createNodeForKey(Distillery c) {
        Node node = new AbstractNode(Children.LEAF, Lookups.singleton(c)) {

            @Override
            public Action[] getActions(boolean context) {
                Action[] result = new Action[]{
                    SystemAction.get(DeleteAction.class),
                    SystemAction.get(PropertiesAction.class)
                };
                return result;
            }

            @Override
            public boolean canDestroy() {
                Distillery distillery = this.getLookup().lookup(Distillery.class);
                return distillery != null;
            }

            @Override
            public void destroy() throws IOException {
                if (deleteIlo(this.getLookup().lookup(Distillery.class).getDistilleryId())) {
                    super.destroy();
                    DistilleryExplorerTopComponent.refreshNode();
                }
            }

        };
        node.setDisplayName(c.getName());
//        node.setShortDescription(c.getPackage1());
        return node;
    }

    private static boolean deleteIlo(long iloId) {
        
        return true;
    }

}
